/*******************************************************************************
 * Copyright (c) 2013, 2014 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    EclipseSource - initial API and implementation
 ******************************************************************************/
package org.apache.hop.ui.hopgui;

import java.nio.file.Files;
import java.nio.file.Paths;
import org.apache.commons.vfs2.FileObject;
import org.apache.hop.ui.core.PropsUi;
import org.apache.hop.ui.core.dialog.BaseDialog;
import org.apache.hop.ui.core.gui.GuiResource;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.json.JSONObject;

public class Webssh extends Dialog {
  private static final Class<?> PKG =
      org.apache.hop.ui.core.dialog.ShowBrowserDialog.class; // For Translator

  private final String dialogTitle;
  private Shell shell;
  private FileObject fileObj;
  private Browser wBrowser;

  public Webssh(Shell parent, String dialogTitle) {
    super(parent, SWT.NONE);
    this.dialogTitle = dialogTitle;
  }

  /**
   * 设置文件
   *
   * @param fileObj 文件对象
   */
  public void setFileObj(FileObject fileObj) {
    this.fileObj = fileObj;
  }

  public void open() {
    Shell parent = getParent();
    shell = new Shell(parent, SWT.MAX | SWT.RESIZE | SWT.TOP);
    shell.setImage(GuiResource.getInstance().getImageHopUi());
    PropsUi.setLook(shell);
    shell.setText(dialogTitle);
    createStyleControls(shell);
    BaseDialog.defaultShellHandling(shell, c -> ok(), c -> ok());
  }

  public void dispose() {
    shell.dispose();
  }

  private void ok() {
    dispose();
  }

  /**
   * 创建窗口
   *
   * @param parent
   */
  protected void createStyleControls(Shell parent) {
    parent.setLayout(new FormLayout());
    wBrowser = new Browser(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    FormData fdBrowser = new FormData();
    fdBrowser.left = new FormAttachment(0, 0);
    fdBrowser.top = new FormAttachment(0, 0);
    fdBrowser.right = new FormAttachment(100, 0);
    fdBrowser.bottom = new FormAttachment(100, 0);
    wBrowser.setLayoutData(fdBrowser);

    String path = this.fileObj.getPath().toString();
    try {
      String content = new String(Files.readAllBytes(Paths.get(path)));
      JSONObject jsonObject = new JSONObject(content);
      String url = jsonObject.getString("url");
      path = path.replace("file:///", "/");
      String file = path;
      // wBrowser.setUrl("http://8.130.66.117:5174/?file=" + file);
      // wBrowser.setUrl("http://10.220.130.88:11409/?file=" + file);
      wBrowser.setUrl(url + "?file=" + file);
    } catch (Exception e) {
      MessageBox box = new MessageBox(parent);
      box.setMessage("文件格式错误");
      box.open();
    }
    // int index = path.lastIndexOf("/BOOT-INF/");
    // String file = path.substring(index);

  }
}
